<?php
namespace Admin\Models\sys;

use Admin\Models\BaseModel;

class XhsysUserextrapower extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $id;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     * @explain 对象，1=user，2=group
     */
    public $iGoal;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     * @explain 对象id
     */
    public $iGoalId;

    /**
     *
     * @var string
     * @Column(type="string", length=50, nullable=true)
     * @explain 类型(dep/userGroup)
     */
    public $sType;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bManage;

    /**
     *
     * @var string
     * @Column(type="string", length=50, nullable=true)
     * @explain 目标(customer)
     */
    public $sTarget;

    /**
     *
     * @var string|array
     * @Column(type="string", nullable=true)
     */
    public $mContent;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bAll;

    /**
     *
     * @var integer
     * @Column(type="integer", length=1, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSys_UserExtraPower';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsysUserextrapower[]|XhsysUserextrapower
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsysUserextrapower
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public function beforeSave()
    {
        $this->mContent = json_encode(array_values(array_filter($this->mContent ?: [])));
        $this->bAll     = ($this->bAll == 'true' || $this === true) ? 1 : 0;
        return true;
    }

    public function afterFetch()
    {
        $this->mContent = $this->mContent ? json_decode($this->mContent, true) : [];
        $this->bAll     = $this->bAll ? true : false;
        return true;
    }

}
